<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of StreetTeamModel
 *
 * @author Admin
 */
class Application_Model_StreetTeamModel extends Zend_Db_Table_Abstract {

    protected $_name = 'street_team';
    protected $_fields = 'id,switch_id,user_id';
    public $adapter;

    function init() {
        $this->adapter = $this->getAdapter();
    }

    function save($data) {
        if (isset($data['id'])) {
            return $this->update($data, "id = $data[id]");
        } else {
            if (!$this->recordExist($data))
                return $this->insert($data);
        }
    }

    function saveSwitchUsers($switch_id, $users) {
        $data = array();
        $data['switch_id'] = $switch_id;
        $this->adapter->delete($this->_name, "switch_id = '$switch_id'");
        foreach ($users as $user) {
            $data['user_id'] = $user;
            $this->save($data);
        }
    }

    function recordExist($data) {
        return $this->adapter->fetchRow("select id from $this->_name where switch_id = '{$data['switch_id']}' AND user_id = '{$data['user_id']}'");
    }

}